<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 孤焰
  Date: 2021/6/15
  Time: 17:33
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>订单增加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">订单增加</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">收货地址</label>
                    <div class="layui-input-block">
                        <select name="shippingId">
                            <option value=""></option>
                            <c:forEach var="shipping" items="${shippingList}">
                                <option value="${shipping.id}">${shipping.receiverProvince}${shipping.receiverCity}${shipping.receiverDistrict}${shipping.receiverAddress}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">实际付款金额</label>
                    <div class="layui-input-block">
                        <input type="text" name="payment" lay-verify="payment" autocomplete="off" placeholder="请输入实际付款金额"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">支付类型</label>
                    <div class="layui-input-block">
                        <select name="paymentType">
                            <option value=""></option>
                            <option value="1">在线支付</option>
                            <option value="2">线下支付</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">运费</label>
                    <div class="layui-input-block">
                        <input type="text" name="postage" lay-verify="postage" autocomplete="off" placeholder="请输入运费"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">订单状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="0" title="已取消" checked="">
                        <input type="radio" name="status" value="1" title="未付款">
                        <input type="radio" name="status" value="2" title="已付款">
                        <input type="radio" name="status" value="3" title="已发货">
                        <input type="radio" name="status" value="4" title="交易成功">
                        <input type="radio" name="status" value="5" title="交易关闭">
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="order-submit">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/static/js/axios.min.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var layer = layui.layer;
        var form = layui.form;

        // 自定义表单验证
        form.verify({
            payment: [/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/, '价格值不合法'],
            postage: [/^[1-9]\d*$/, '运费必须为正整数']
        });

        /* 监听提交 */
        form.on('submit(order-submit)', function (data) {
            console.log(data);
            var order = data.field;
            order.userId = ${user.id}

            if (!order.shippingId) {
                layer.msg('请选择收货地址', {icon: 5});
                return false;
            }

            if (!order.paymentType) {
                layer.msg('请选择支付类型', {icon: 5});
                return false;
            }

            axios.post('${pageContext.request.contextPath}/order/addOrder', order).then(function (res) {
                // TODO 添加成功之后跳转至商品列表页存在BUG 如果商品列表页存在 则需要手动刷新
                if (res.data.code == 200) {
                    top.layui.index.openTabsPage('${pageContext.request.contextPath}/order/orderPage', "订单列表");
                } else {
                    layer.msg(res.data.message, {icon: 5});
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
